//
//  HMWelcomeViewController.swift
//  iOSWeibo11
//
//  Created by Apple on 16/7/21.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

/// 欢迎界面
class HMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
        // 获取用户信息之前显示一次用户头像
        self.setIcon()
        
        // 获取用户信息
        HMUserAccountViewModel.sharedUserAccountViewModel.loadUserInfo { (error) -> () in
            if error != nil {
                print("获取用户信息失败")
                return
            }
            
            // 获取信息成功了
            self.setIcon()
//            if let avatar_large = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
//                // 设置图片是在加载完用户数据后才去设置,加载用户数据慢,前面不会显示出这个头像
//                // 使用SDWebImage下载图片
//                let url = NSURL(string: avatar_large)
//                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
//            }
        }
    }
    
    /// 设置头像
    private func setIcon() {
        if let avatar_large = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large {
            // 设置图片是在加载完用户数据后才去设置,加载用户数据慢,前面不会显示出这个头像
            // 使用SDWebImage下载图片
            let url = NSURL(string: avatar_large)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    /// 头像动画
    private func startAnimation() {
        let offset = -(UIScreen.mainScreen().bounds.height - 160)
        // snp_updateConstraints修改约束,注意,更新约束参照不能改变
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view).offset(offset)
        }
        
//        UIView.animateWithDuration(0.25) { () -> Void in
//            self.view.layoutIfNeeded()
//        }
        
        /// 带弹簧效果的动画
        // delay: 延迟时间
        // usingSpringWithDamping:弹簧明显效果 0 - 1
        // initialSpringVelocity: 初速度
        UIView.animateWithDuration(1, delay: 0.2, usingSpringWithDamping: 0.75, initialSpringVelocity: 7, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                // 动画完成
                print("动画完成")
                
                UIView.animateWithDuration(DefaultAnimtionDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 切换到 mianTabBarVC
                        AppDelegate.switchRootViewController(HMMainTabBarController())
//                        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
//                        appDelegate.window?.rootViewController = HMMainTabBarController()
                })
        }
    }
    
    /// 设置UI界面
    private func setupUI() {
        // 1.添加子控件
        self.view.addSubview(bkgImageView)
        self.view.addSubview(iconView)
        self.view.addSubview(welcomeLabel)
        
        // 2.添加约束 Masonry -> SnapKit
        // makeConstraints 添加约束
        // 背景,填充父控件
        bkgImageView.snp_makeConstraints { (make) -> Void in
            // 参照的属性需要添加snp_
//            make.leading.equalTo(self.view.snp_leading)
//            make.top.equalTo(self.view.snp_top)
//            make.trailing.equalTo(self.view.snp_trailing)
//            make.bottom.equalTo(self.view.snp_bottom)
            
            // 简写1, 当要约束的属性和参照的属性一样时,参照的约束可以省略
//            make.leading.equalTo(self.view)
//            make.top.equalTo(self.view)
//            make.trailing.equalTo(self.view)
//            make.bottom.equalTo(self.view)

            // 简写2,当多个约束参照都是一样的时候可以写在一起.
//            make.leading.equalTo(self.view)
//            make.top.equalTo(self.view)
//            make.trailing.equalTo(self.view)
//            make.bottom.equalTo(self.view)

//            make.leading.top.trailing.bottom.equalTo(self.view)
            
            // 填充父控件
            make.edges.equalTo(self.view)
//            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        // label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }

    // MARK: - 懒加载控件
    /// 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        imageView.contentMode = UIViewContentMode.ScaleAspectFill
        
        return imageView
    }()
    
    /// 欢迎label
    private lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(18)
        
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
    }()
}
